package com.wes.dsptest.dspcontrol;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.wes.dsptest.R;
import com.xw.repo.BubbleSeekBar;

public class EqualizerView extends FrameLayout{
    public final static int TYPE_FREQ = 0;
    public final static int TYPE_Q = 1;
    public final static int TYPE_GAIN = 2;

    private TextView tvTitle;
    private TextView tvFreqVal, tvQval, tvGainVal;
    private TextView tvFreqMax, tvFreqMin, tvQValMax, tvQValMin,tvGainMax, tvGainMin;
    private BubbleSeekBar sbFreq, sbQval, sbGain;
    private EqSliderChangeListener vListener;
    private BubbleSeekBar.OnProgressChangedListener listener = new BubbleSeekBar.OnProgressChangedListener() {
        @Override
        public void onProgressChanged(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat, boolean fromUser) {
            if (vListener != null) {
                int type = TYPE_FREQ;
                if(bubbleSeekBar == sbQval)
                    type = TYPE_Q;
                else if(bubbleSeekBar == sbGain)
                    type = TYPE_GAIN;
                vListener.onChange(type,progress, fromUser);
            }
        }

        @Override
        public void getProgressOnActionUp(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {

        }

        @Override
        public void getProgressOnFinally(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat, boolean fromUser) {

        }
    };


    public EqualizerView(@NonNull Context context) {
        super(context);
        init();
    }

    public EqualizerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EqualizerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public EqualizerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public void init(){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.audio_dsp_eq_view, this, false);
        tvTitle = view.findViewById(R.id.title);
        tvFreqVal = view.findViewById(R.id.tv_freq_val);
        tvFreqMax = view.findViewById(R.id.tv_freq_max);
        tvFreqMin = view.findViewById(R.id.tv_freq_min);
        tvQValMax = view.findViewById(R.id.tv_q_max);
        tvQValMin = view.findViewById(R.id.tv_q_min);
        tvGainMax = view.findViewById(R.id.tv_gain_max);
        tvGainMin = view.findViewById(R.id.tv_gain_min);
        tvQval = view.findViewById(R.id.tv_q_val);
        tvGainVal = view.findViewById(R.id.tv_gain_val);
        sbFreq = view.findViewById(R.id.sb_frq);
        sbQval = view.findViewById(R.id.sb_q_val);
        sbGain = view.findViewById(R.id.sb_gain);
        sbFreq.setOnProgressChangedListener(listener);
        sbQval.setOnProgressChangedListener(listener);
        sbGain.setOnProgressChangedListener(listener);
        addView(view);
    }

    public BubbleSeekBar setSeekBar(int type){
        BubbleSeekBar bsb = sbFreq;
        switch (type)
        {
            case TYPE_Q:
                bsb = sbQval;
                break;
            case TYPE_GAIN:
                bsb = sbGain;
                break;
        }
        return bsb;
    }


    @SuppressLint("ResourceAsColor")
    public TextView setTextViewVal(int type){
        TextView text = tvFreqVal;
        switch (type)
        {
            case TYPE_Q:
                text = tvQval;
                break;
            case TYPE_GAIN:
                text = tvGainVal;
                break;
        }
        return text;
    }

    public void setName(CharSequence string) {
        tvTitle.setText(string);
    }

    public void setMax(int type, int val){
        setSeekBar(type).getConfigBuilder().max((float) val).build();
        updateProgressMaxMinText(type, val, true);
    }

    public void setMin(int type, int val){
        setSeekBar(type).getConfigBuilder().min((float) val).build();;
        updateProgressMaxMinText(type, val, false);
    }

    public void setCurrentVal(int type, int progress) {
        setCurrentVal(type, progress, false);
    }

    public void updateProgressMaxMinText(int type, int val, boolean isMax) {
        TextView tvNow = tvFreqMax;
        String text = "null";
        switch (type){
            case TYPE_FREQ:
                if(isMax)
                    tvNow = tvFreqMax;
                else
                    tvNow = tvFreqMin;
                text = val + "Hz";
                break;
            case TYPE_Q:
                if(isMax)
                    tvNow = tvQValMax;
                else
                    tvNow = tvQValMin;
                text = String.valueOf(val);
                break;
            case TYPE_GAIN:
                if(isMax)
                    tvNow = tvGainMax;
                else
                    tvNow = tvGainMin;
                text = val + "dB";
                break;
        }
        tvNow.setText(text);

    }
    public void updateProgressText(int type, int progress) {
        BubbleSeekBar sb = setSeekBar(type);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if(type == TYPE_FREQ)
                setTextViewVal(type).setText(" : " + (progress - sb.getMin()) +"Hz");
            else if(type == TYPE_GAIN)
                setTextViewVal(type).setText(" : " + (progress - sb.getMin()) +"dB");
            else
                setTextViewVal(type).setText(" : " + (progress - sb.getMin()));
        } else {
            if(type == TYPE_FREQ)
                setTextViewVal(type).setText(" : " + progress + "/" + sb.getMax() +"Hz");
            else if(type == TYPE_GAIN)
                setTextViewVal(type).setText(" : " + progress + "/" + sb.getMax() +"dB");
            else
            setTextViewVal(type).setText(" : " + progress + "/" + sb.getMax());
        }
    }

    public void setCurrentVal(int type, int progress, boolean animated) {
        setSeekBar(type).setProgress(progress);
        updateProgressText(type, progress);
    }

    public void setListener(EqSliderChangeListener vListener) {
        this.vListener = vListener;
    }

    public interface EqSliderChangeListener {
        void onChange(int type, int val, boolean fromUser);
    }
}
